﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.Diagnostics;
using CommonClass;
using EncryptProject;
using RegistryLib;
using CommonBase;
using SurveillanceLib;


namespace ResetTrialTimeProject
{
    public partial class frm_Main : Form
    {
        #region Const Fields

        private const int MAX_RESET_TIMES = 5; // 最多重置次数

        #endregion

        #region Private Fields

        private SurveillanceDAL dataOperation = new SurveillanceDAL(); // 操作数据库
        private RegistryOperation regOperation = new RegistryOperation(); // 操作注册表
        private int resetTimes = 0; // 从数据库获取剩余重置次数

        #endregion

        #region 构造函数

        public frm_Main()
        {
            InitializeComponent();
        }

        #endregion

        #region 事件

        /// <summary>
        /// 初始化窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frm_Main_Load(object sender, EventArgs e)
        {
            try
            {
                #region 生成唯一机器码
                Utilities.GenerateMachineCode();
                #endregion

                DataSet dsSystem = dataOperation.GetDataSet(SurveillanceDAL.QUERY_GET_ALLRESETDATA,
                    string.Empty, "db_VWMSConnectionString", string.Format("{0}.exe", Application.ProductName));
                if (dsSystem.Tables.Count > 0)
                {
                    // 比较数据库与注册表的剩余重置次数，取两者最小值
                    resetTimes = MAX_RESET_TIMES - dsSystem.Tables[0].Rows.Count; // 从数据库获取剩余重置次数
                    if (resetTimes < 0 || resetTimes > MAX_RESET_TIMES)
                    {
                        resetTimes = 0; // 没有重置次数
                    }
                    else // 再判断注册表中的值
                    {
                        regOperation.CreateSubKey(); // 创建子项，若存在该项时无任何操作
                        object regValue = regOperation.GetSubKeyValue(null, null,
                            RegistryOperation.SUBKEYNAME_RESETTIMES); // 读注册表中的重置次数
                        int outResetTimes = MAX_RESET_TIMES; // 从注册表获取剩余重置次数

                        if (regValue != null) // 注册表中有值
                        {
                            try
                            {
                                outResetTimes = MAX_RESET_TIMES - int.Parse(EncryptAlgorithmHelper.Decrypt(regValue.ToString(),
                                    Utilities.MachineCode));
                            }
                            catch (System.Exception ex)
                            {
#if DEBUG
                                Debug.WriteLine(ex.Message);
#endif
                                resetTimes = outResetTimes = 0; // 没有重置次数
                            }
                        }

                        resetTimes = Math.Min(resetTimes, outResetTimes); // 取两者最小值
                        if (resetTimes != 0) // 还有重置次数时
                        {
                            btnReset.Enabled = true;
                        }
                    }

                    lbResetTime.Text = string.Format("{0}次", resetTimes.ToString());
                }
                else
                {
#if DEBUG
                    throw new DataException("数据库异常！");
#endif
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
                Application.Exit();
            }
        }

        /// <summary>
        /// 重置次数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            try
            {
                // 1. 重置次数
                // a. 重置数据库
                int resetResult = dataOperation.ResetTrialTimes(EncryptAlgorithmHelper.Encrypt2("0", Utilities.MachineCode));
                if (resetResult != 0)
                {
                    // b. 重置注册表
                    regOperation.SetSubKeyValue(null, null, RegistryOperation.SUBKEYNAME_TRIALTIMES,
                        EncryptAlgorithmHelper.Encrypt2("0", Utilities.MachineCode));

                    // 2. 写入正确的重置次数到数据库与注册表
                    // a. 写入数据库
                    int addResult = dataOperation.AddResetItem();
                    if (addResult != 0)
                    {
                        // b. 写入注册表
                        regOperation.SetSubKeyValue(null, null, RegistryOperation.SUBKEYNAME_RESETTIMES,
                            EncryptAlgorithmHelper.Encrypt((MAX_RESET_TIMES - resetTimes + 1).ToString(), Utilities.MachineCode)); // 存已使用最大重置次数

                        MessageBox.Show("重置成功！", ConstantString.MSG_COMPANY);
                    }
                    else
                    {
                        MessageBox.Show("重置失败！", ConstantString.MSG_COMPANY);
                    }
                }
                else
                {
                    MessageBox.Show("重置失败！", ConstantString.MSG_COMPANY);
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }
            finally
            {
                Application.Exit();
            }
        }

        #endregion
    }
}
